<?php
/*
 * @Author: shuaijin 780309705@qq.com
 * @Date: 2024-02-20 16:45:42
 * @LastEditors: shuaijin 780309705@qq.com
 * @LastEditTime: 2024-09-29 11:17:41
 * @FilePath: \erp\app\controller\Upload.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */


namespace app\controller;


if (is_file(app_path(). '/vendor/aliyuncs/oss-sdk-php/autoload.php')) {
    require_once app_path(). '/vendor/aliyuncs/oss-sdk-php/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use think\facade\Db;
// composer require aliyuncs/oss-sdk-php
class Upload
{
    public function upload(){
        // 获取表单上传文件
        $files = request()->file();
        try {
            validate(['image'=>'fileSize:10240|fileExt:jpg|image:200,200,jpg'])
                ->check($files);
            $savename = [];
            foreach($files as $file) {
                $savename[] = \think\facade\Filesystem::disk('public')->putFile( 'erp', $file);
            }
            //
            return $savename;
        } catch (\think\exception\ValidateException $e) {
            echo $e->getMessage();
        }
    }

    public function  oss(){
            $msg['code']=100;
            $msg['msg']="error";
            $msg['url']="";
            $msg['id']="";
            $msg['name']="";
            $accessKeyId="LTAI5tQcTz5AS1M3nmNNzdnM";
            $accessKeySecret="sDSsXzueKCxsS6n1Hr6TLZVCwiIXlv";
            $endpoint = "oss-cn-shanghai.aliyuncs.com";
            $bucketName = 'seny-erp';           
            
            // 处理文件上传
            if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
                $file = $_FILES['file'];
            
                // 初始化OSS客户端
                try {
                    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
                } catch (OssException $e) {
                    return json($e->getMessage());
                }
            
                // 生成唯一文件名
                $object = 'uploads/' . uniqid() . md5(rand(1,9999)). "." .strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));

                // 上传文件到OSS
                try {

                    $res = $ossClient->uploadFile($bucketName, $object,$file['tmp_name']);   
                    if($res){
                        $msg['code']=200;
                        $msg['name']=$file['name'];
                        $msg['msg']="success";
                        $msg['url']="https://".$bucketName.".".$endpoint."/".$object; 
                        $msg['id']=Db::table("annex")->insertGetId([
                            'name'=>$file['name'],
                            'url'=>$msg['url'],
                            'status'=>1,
                            'createtime'=>time(),
                        ]);
                    }
                    
                    return json($msg);               
                } catch (OssException $e) {
                    $msg['msg']="报错了";
                    return json($msg);
                }

            }else{
                $msg['msg']="请选择文件";
            }

            return json($msg);          
    }

    public function  minimastoss(){
        $msg['code']=100;
        $msg['msg']="error";
        $msg['url']="";
        $accessKeyId="LTAI5tMGG9meN4iF76mW35CZ";
        $accessKeySecret="Wbnio8MxDBk9RXRnbYD6aLJCssSNwE";
        $endpoint = "oss-cn-shanghai.aliyuncs.com";
        $bucketName = 'mini-mars-master';           
        
        // 处理文件上传
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
            $file = $_FILES['file'];
        
            // 初始化OSS客户端
            try {
                $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            } catch (OssException $e) {
                return json($e->getMessage());
            }
        
            // 生成唯一文件名
            $object = 'uploads/' . uniqid() . md5(rand(1,9999)) .'-' . $file['name'];

            // 上传文件到OSS
            try {

                $res = $ossClient->uploadFile($bucketName, $object,$file['tmp_name']);   
                if($res){
                    $msg['code']=200;
                    $msg['msg']="success";
                    $msg['url']="https://".$bucketName.".".$endpoint."/".$object; 
                }
                
                return json($msg);               
            } catch (OssException $e) {
                return json($msg);
            }

        }

        return json($msg);          
}


}